﻿using AutoMapper;
using IOA.MES.DataAccess.Entity;
using IOA.MES.DataAccess.VModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

namespace IOA.MES.Business
{
    public class CommonMgr
    {
        private MESContext mesDb = new MESContext("MES");

        /// <summary>
        /// /查询枚举类型
        /// </summary>
        /// <returns></returns>
        public List<KeyValuePair<int, string>> QueryEnumTypes(Type enumType)
        {
            var types = new List<KeyValuePair<int, string>>();
            foreach (int myCode in Enum.GetValues(enumType))
            {
                var value = Enum.GetName(enumType, myCode);
                var field = enumType.GetField(value);
                var objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                value = !objs.Any() ? value : ((DescriptionAttribute)objs[0]).Description;
                types.Add(new KeyValuePair<int, string>(myCode, value));
            }
            return types;
        }

        /// <summary>
        /// 查询所有功率档位
        /// </summary>
        /// <param name="workshopCode">车间代码</param>
        /// <returns>功率档列表</returns>
        public List<VBinningPower> QueryPowers(string workshopCode)
        {
            var entities = (from schema in mesDb.BinningSchemas.AsNoTracking()
                            join power in mesDb.BinningPowers.AsNoTracking() on schema.PkId equals power.SchemaId
                            where schema.Valid && schema.WorkshopCode == workshopCode && power.Valid
                            select power).Distinct().ToList();
            return Mapper.Map<List<VBinningPower>>(entities);
        }

        /// <summary>
        /// 查询所有电流档
        /// </summary>
        /// <param name="workshopCode">车间代码</param>
        /// <returns>电流档列表</returns>
        public List<VBinningCurrent> QueryCurrents(string workshopCode)
        {
            var entities = (from schema in mesDb.BinningSchemas.AsNoTracking()
                            join current in mesDb.BinningCurrents.AsNoTracking() on schema.PkId equals current.SchemaId
                            where schema.Valid && schema.WorkshopCode == workshopCode && current.Valid
                            select current).Distinct().ToList();
            return Mapper.Map<List<VBinningCurrent>>(entities);
        }
    }
}
